package service;

import dao.MemberAddressDao;
import entity.Member;
import entity.MemberAddress;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

import java.util.List;

/**
 * @program: Mybatis201759225309
 * @description: MemberAddressDao 实现
 * @author: Xiaozhuoer
 * @create: 2021-04-12 13:21
 **/

public class MemberAddressImpl implements MemberAddressDao {
    @Override
    public int addMemberAddress(MemberAddress memberAddress) {
        SqlSession session = MybatisUtil.getSqlSession();
        session.insert("addMemberAddress",memberAddress);
        session.commit();
        session.close();
        System.out.println("insert finish");
        return 1;
    }

    @Override
    public int addMemberThreeAddress() {
        return 0;
    }

    @Override
    public int updateMemberAddressCity(int member_id, String newCity) {
        SqlSession session = MybatisUtil.getSqlSession();
        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setMember_id(member_id);
        memberAddress.setCity(newCity);
        session.update("updateMemberAddressCity",memberAddress);
        session.commit();
        session.close();
        System.out.println("update finish");
        return 1;
    }

    @Override
    public List<MemberAddress> selectMemberAddressAll() {
        SqlSession session = MybatisUtil.getSqlSession();
        MemberAddress memberAddress = new MemberAddress();
        List<MemberAddress> memberAddressList= session.selectList("selectMemberAddressAll");
        session.commit();
        session.close();
        System.out.println("selectMemberAddressAll finish");
        return memberAddressList;
    }

    @Override
    public List<MemberAddress> updateMemberAddressSet(int member_id, String province, String city, String address, String receiver) {
        SqlSession session = MybatisUtil.getSqlSession();
        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setMember_id(member_id);
        memberAddress.setReceiver(province);
        memberAddress.setCity(city);
        memberAddress.setAddress(address);
        memberAddress.setReceiver(receiver);

        List<MemberAddress> memberAddressList= session.selectList("updateMemberAddressSet",memberAddress);
        session.commit();
        session.close();
        System.out.println("updateMemberAddressSet finish");
        return memberAddressList;
    }


    @Override
    public List<MemberAddress> selectMemberAddressForeach(List<String> provinces) {
        SqlSession session = MybatisUtil.getSqlSession();
        MemberAddress memberAddress = new MemberAddress();
        List<MemberAddress> memberAddressList= session.selectList("selectMemberAddressForeach",provinces);
        session.commit();
        session.close();
        System.out.println("selectMemberAddressForeach finish");
        return memberAddressList;
    }
}
